package com.xiaozheng.service.impl;

import com.xiaozheng.dao.AccountDao;
import com.xiaozheng.domain.Account;
import com.xiaozheng.service.AccountService;

import java.util.List;

/**
 * @Package: com.xiaozheng.service.impl
 * @ClassName: AccountServiceImpl
 * @Author: 小政同学    QQ:xiaozheng666888@qq.com
 * @CreateTime: 2020/9/25 12:43
 * @What_is_this_file_for: 这个文件是用来做什么的?
 * @Description: 描述
 */
public class AccountServiceImpl implements AccountService {

    private AccountDao dao;

    public void setDao(AccountDao dao) {
        this.dao = dao;
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Account> findAllAcount() {
        List<Account> allAcount = dao.findAllAcount();
        return allAcount;
    }

    /**
     * 根据id查询一个
     *
     * @return
     */
    @Override
    public Account findAccountById(Integer AccountId) {
        Account accountById = dao.findAccountById(AccountId);
        return accountById;
    }

    /**
     * 保存
     *
     * @param account
     */
    @Override
    public void saveAccount(Account account) {
        dao.saveAccount(account);
    }

    /**
     * 更新
     *
     * @param account
     */
    @Override
    public void updateAccount(Account account) {
        dao.updateAccount(account);
    }

    /**
     * 根据id删除
     *
     * @param AccountId
     */
    @Override
    public void deleteAccount(Integer AccountId) {
        dao.deleteAccount(AccountId);
    }

    /**
     * 补充共功能
     * 转账
     *
     * @param sourceUid 转出账户名称
     * @param targetUid 转入账户名称
     * @param money     转账金额
     */
    @Override
    public void transferAccount(String sourceUid, String targetUid, Float money) {
        try {
            //2.执行操作

            System.out.println("开始！！");
            //2.1根据名称查询转出账户
            Account sourceuid = dao.findByUId(sourceUid);
            //2.2根据名称查询转入账户
            Account targetuid = dao.findByUId(targetUid);
            //2.3转出账户减钱
            sourceuid.setMoney(sourceuid.getMoney() - money);
            int i = 1/0;
            //2.4转入账户增钱
            targetuid.setMoney(targetuid.getMoney() + money);
            //2.5更新转入账户
            dao.updateAccount(targetuid);
            //2.6更新转出账户
            dao.updateAccount(sourceuid);
            System.out.println("结束");
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

}
